package com.ngovanhau.justsort.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.graphics.Point;

import com.ngovanhau.justsort.item.config.MyConfig;
import com.ngovanhau.justsort.mylog.MyLog;

public class Matrix2D {
	public static int[][] matrix;
	
	public static void init(){
		matrix = new int[LevelSetting.rowLevel][LevelSetting.columnLevel];
		createFirstMatrix2D();
	}
	
	public static void createFirstMatrix2D(){
		List<Integer> array = new ArrayList<Integer>();
		for(int i=0; i<LevelSetting.rowLevel*LevelSetting.columnLevel; i++){
			array.add(i+1);
		}
		for(int i = 0; i<LevelSetting.rowLevel; i++){
			for(int j =0; j<LevelSetting.columnLevel; j++){
				Collections.shuffle(array);
				matrix[i][j]= array.get(0);
				array.remove(0);
			}
		}
	}
	
	public static void resetMatrix2D(){
		matrix = new int[LevelSetting.rowLevel][LevelSetting.columnLevel];
		List<Integer> array = new ArrayList<Integer>();
		for(int i=0; i<LevelSetting.rowLevel*LevelSetting.columnLevel; i++){
			array.add(i+1);
		}
		for(int i = 0; i<LevelSetting.rowLevel; i++){
			for(int j =0; j<LevelSetting.columnLevel; j++){
				Collections.shuffle(array);
				matrix[i][j]= array.get(0);
				array.remove(0);
			}
		}
	}
	
	public static void showMatrix2D(int[][] mt){
		for(int i =0; i< LevelSetting.rowLevel ; i++){
			for(int j = 0; j<LevelSetting.columnLevel; j++){
				MyLog.print(" " + i );
			}
			MyLog.println("");
		}
	}
	
	public static int[][] getMatrix2D(){
		return matrix;
	}
	
	public static void setMatrix2D(int[][] mt){
		matrix = mt;
	}
	
	public static void setPxPy(int i, int j, int v){
		matrix[i][j] = v;
	}
	
	public static Point getPxPy(int i, int j){
		int Px = (int)(MyConfig.X_START + j * MyConfig.WIDTH_NUMBER);
		int Py = (int)(MyConfig.SCREENHEIGHT/2 - MyConfig.SCREENWIDTH/2  + i * MyConfig.HEIGHT_NUMBER);
		return new Point(Px,Py);
	}
	
	public static Point getCell(int x, int y){
		int i = (int)((y-MyConfig.Y_START) / MyConfig.HEIGHT_NUMBER);
		int j = (int) ((x-MyConfig.X_START)/ MyConfig.WIDTH_NUMBER);
		return new Point(i,j);
	}
}
